package com.alinesno.cloud.initializr.starter.gateway.rest;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.initializr.starter.entity.BuildApplicationTemplatesEntity;
import com.alinesno.cloud.initializr.starter.gateway.aop.DataFilter;
import com.alinesno.cloud.initializr.starter.gateway.utils.Utils;
import com.alinesno.cloud.initializr.starter.service.IBuildApplicationTemplatesService;
import com.alinesno.cloud.rest.gateway.adapter.page.TableDataInfo;
import com.alinesno.cloud.rest.gateway.adapter.plugins.TranslateCode;
import com.alinesno.cloud.rest.gateway.adapter.rest.BaseRestAdapter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
/**
 * 【请填写功能名称】Controller
 *
 * @author alinesno
 * @date 2021-06-05
 */
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("/com/alinesno/cloud/business/buildApplicationTemplate" )
public class BuildApplicationTemplatesRest extends BaseRestAdapter<BuildApplicationTemplatesEntity , IBuildApplicationTemplatesService> {

    //日志记录
    private static final Logger log = LoggerFactory.getLogger(BuildApplicationTemplatesRest.class);
    @Autowired
    private IBuildApplicationTemplatesService buildApplicationTemplatesService;

    @DataFilter
    @TranslateCode
    @ResponseBody
    @PostMapping("/datatables")
    public TableDataInfo datatables(HttpServletRequest request , Model model ,  DatatablesPageBean page){
        log.debug("page = {}" , ToStringBuilder.reflectionToString(page));
		return this.toPage(model, this.getFeign() , page) ;
    }
    
    /**
	 * 初始化
	 * @return
	 */
	@GetMapping("/getTemplate")
	public AjaxResult getTemplate(String businessType) {
		AjaxResult r = AjaxResult.success() ;
		
		QueryWrapper<BuildApplicationTemplatesEntity> queryWrapper = new QueryWrapper<>() ; 
		queryWrapper.eq("business_type", businessType) ; 
		List<BuildApplicationTemplatesEntity> templates = buildApplicationTemplatesService.list(queryWrapper) ; 
		
		r.put("templates", templates) ; 
		return r ; 
	}

	/**
	 * 获取上传文件的内容
	 * @return
	 * @throws IOException 
	 */
	@PostMapping("/uploadTemplateContent")
	public AjaxResult uploadTemplateContent(@RequestParam("file") MultipartFile file) throws IOException {
		String content = IOUtils.toString(file.getInputStream() ,  Charset.defaultCharset()) ; 
		log.debug("content = {}" , content) ;
		Object json = Utils.convertToYaml(content) ; 
		return AjaxResult.success("操作成功" , JSONObject.toJSONString(json)) ; 
	}
	
    @Override
    public IBuildApplicationTemplatesService getFeign() {
        return this.buildApplicationTemplatesService;
    }
}
